# -*- coding: utf-8 -*-
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import render_to_response
from django.template import RequestContext
from modeltranslation.admin import TranslationAdmin
from .. import models
from forms import StructureNodeAdminForm


class StructureNodeExtendFieldInline(admin.TabularInline):
    model = models.StructureNodeExtendField


class StructureNodeAdmin(TranslationAdmin):
    form = StructureNodeAdminForm
    list_display = ('get_structure_name', 'get_path')

    exclude = ('parent',)
    inlines = (StructureNodeExtendFieldInline,)
    
    def get_structure_name(self, obj):
        return '&nbsp;'* (10*obj.level) + u' &mdash; ' + obj.title
    get_structure_name.allow_tags=True 

    def get_path(self, obj):
        return '<a href="%s" target="_blank">%s</a>' % (obj.path, obj.path)
    get_path.allow_tags=True 

    def _changelist_view(self, request, extra_context=None):
        app_label = self.model._meta.app_label
        context = {'title': _(u'Структура сайта'),
                   'object_list': models.StructureNode.objects.order_by('tree_id', 'left'),
                   'has_add_permission': self.has_add_permission(request),
                   'root_path': self.admin_site.root_path,
                   'app_label': app_label,
                   }
        context.update(extra_context or {})
        return render_to_response(self.change_list_template, context, 
                                  context_instance=RequestContext(request))
        
  
class PhotoInline(admin.TabularInline):
    fields = ('photo', 'weight', 'is_main')
    model = models.Photo
    extra = 10
    sortable_field_name = "weight"

class AlbumAdmin(admin.ModelAdmin):
    inlines = (PhotoInline,)

admin.site.register(models.Album, AlbumAdmin)
admin.site.register(models.StructureNode, StructureNodeAdmin)